var rdmImg = function () {
    var width = Math.floor(Math.random() * 100) + 300,
        height = Math.floor(Math.random() * 500) + 300;
    return "http://placekitten.com/" + height + '/' + width;
};

// 获取图片
function getImg() {
    var arr = new Array();
    for (var i = 0; i < 20; i++) {
        var img = {};
        img.src = rdmImg();
        arr.push(img);
    }
    return arr;
}


export default {
    data() {
        return {
            photos: false,
            pageNum: 1,
            curPage: 0,
            pageSize: 10,
            showPage: false,
        }
    },
    beforeRouteEnter (to, from, next) {
        next(vm => {
            // vm.init();
        })
    },
    mounted() {
        this.init();
    },
    watch: {
      '$route' (to, from) {
         if(from.name != 'detail') {
            this.init();
        }
      }
    },
    methods: {
        init() {
            if(this.$route.name == 'list') {
              let _this = this;
              _this.$util.setTitle('大赞门城');
              _this.photos= false;
              _this.pageNum = 1;
              _this.curPage = 0;
              _this.pageSize = 10 
              _this.queryPhotoList();
              _this.$util.droploadUpOnScroll(this.queryPhotoList);
          }  
        },
        /**
         * 获取列表
         */ 
        queryPhotoList() {
            //如果当前页==下一页则不发请求
            if (this.pageNum == this.curPage) {
                 return 
            }else { 
                this.curPage = this.pageNum; 
            }
            let _this = this;
            var params = {
                "pageNum": _this.pageNum,
                "pageSize": _this.pageSize
            };
             debugger
            this.$http.post("/praisingCity/getPhotoList", params).then(function (rep) {
                if(rep.meta && rep.meta.code == '0') {
                    let datas = rep.data || [];
                    if( !_this.photos ){
                        _this.photos=[]
                    }
                    _this.photos = _this.photos.concat(datas)
                    
                    _this.arrlist();   
                    if (datas.length >= _this.pageSize) {
                        //是否有下一页
                        _this.pageNum++;
                    }
                }
            }).catch(function(err){
                console.log(err);
            })
        },
        /**
         * 获取图片 
         */
        getImgs() {
            let arr = getImg();
            this.photos = this.photos.concat(arr)
            this.arrlist();
        },
        
        /**
         * 点赞 
         */
        praise(item) {
            let _this = this;
            let param = {
                "id": item.id
            }
            _this.$http.post('praisingCity/praise',param).then(function(rep){
                if(rep.meta && rep.meta.code == '0') {
                    item.praised = !item.praised;
                    if(item.praised) {
                        item.praiseCount = item.praiseCount - 0 + 1; 
                    }else {
                       item.praiseCount = item.praiseCount - 0 - 1; 
                    }
                    
                }
            }).catch(function() {

            })
        },
        /**
         * 跳转详情
         */
        godetail(id) {
            this.$router.push({'name': 'detail',params:{'id':id}})
        }, 

        goPhoto(){
            let code = sessionStorage.getItem('toon_code');
            let url = configM.comUrl + '/conv-photo/index.html?code='+ code
            window.location.href = url;
        },
        /**
         * 排序 
         */
        arrlist() {
            let _this = this,
                $l = $("#waterfalls-left"),
                $r = $("#waterfalls-right");
            _this.leftHeight = 0;
            _this.rightHeight = 0;
            setTimeout(function () {
                console.log($("#items>div"));
                $("#items>div").each(function (index, val) {
                    var $this = $(this);
                    var $img = $this.find("img").first();
                    $img.on("error", function () {
                        // $img.attr("src", "../src/assets/images/default.png");
                    });
                
                    if ($img.get(0).complete) {
                        addItems();                        
                    } else {
                        $img.on("load error", function () {
                            addItems();
                        });
                    }
                
                    function addItems() {
                        if (_this.leftHeight <= _this.rightHeight) {
                            $l.append($this);
                            _this.leftHeight = $l.height();
                        } else {
                            $r.append($this);
                            _this.rightHeight = $r.height();
                        }
                    }
                });
            }, 0);
        }
    }


}